package com.example.knowman.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.knowman.ReturnMessage.Message;
import com.example.knowman.ReturnMessage.MessageExRootList;
import com.example.knowman.entity.rootList;
import com.example.knowman.service.IrootListService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/rootList")
public class rootListController {
    private final IrootListService irootListService;

    public rootListController(IrootListService irootListService) {
        this.irootListService = irootListService;
    }

    //查
    @GetMapping("getByUsername")
    public ResponseEntity<rootList> getByUsername(@RequestParam String username) {
        return ResponseEntity.ok(irootListService.getByUsername(username));
    }

    @GetMapping("getAll")
    public ResponseEntity<MessageExRootList> getProblemRes(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                  @RequestParam(required = false) String username) {

        IPage<rootList> res = irootListService.getAll(pageNum, pageSize, username);
        MessageExRootList resMessage = new MessageExRootList();
        resMessage.setCode(200);
        resMessage.setTip("查询成功");
        resMessage.setList(res);
        return ResponseEntity.ok(resMessage);
    }

    //改
    @GetMapping("updateByUsername")
    public ResponseEntity<Message> updateDifficultyRecord(@RequestParam(required = true) String username,
                                                          @RequestParam(required = false) String realname,
                                                          @RequestParam(required = false) Integer rootType) {
        Message res =irootListService.updateByUsername(username, realname, rootType);
        if (res.getCode() == 200){
            return ResponseEntity.ok(res);
        }
        else {
            return ResponseEntity.badRequest().body(res);
        }

    }

    //增
    @GetMapping("InsertWithRootList")
    public ResponseEntity<Message> commit(@RequestParam String username,
                                         @RequestParam(required = false) String realname,
                                         @RequestParam Integer rootType,
                                         @RequestParam(required = false) String passwd) {
        Message res = irootListService.commit(username,passwd,realname,rootType);
        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }

    //
    @GetMapping("DeleteByUsername")
    public ResponseEntity<Message> delete(@RequestParam String username) {
        Message res = irootListService.deleteByUsername(username);
        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }
}
